package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.Orders;
import com.sky.vo.OrderVO;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;


@Mapper
public interface OrderMapper {
    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into orders (number, status, user_id, address_book_id, order_time, checkout_time, pay_method, pay_status, amount, remark, phone, address, user_name, consignee, cancel_reason, rejection_reason, cancel_time, estimated_delivery_time, delivery_status, delivery_time, pack_amount, tableware_number, tableware_status) VALUES (#{number},#{status},#{userId},#{addressBookId},#{orderTime},#{checkoutTime},#{payMethod},#{payStatus},#{amount},#{remark},#{phone},#{address},#{userName},#{consignee},#{cancelReason},#{rejectionReason},#{cancelTime},#{estimatedDeliveryTime},#{deliveryStatus},#{deliveryTime},#{packAmount},#{tablewareNumber},#{tablewareStatus})")
    void insert(Orders orders);
    /**
     * 根据订单号查询订单
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    Page<OrderVO> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);
    @Select("select * from orders where user_id=#{userId}")
    Page<Orders> pageQueryByUserId(Long userId);
    @Select("select * from orders where id=#{id}")
    Orders getById(Long id);
    @Update("update orders set status=6 where id=#{id}")
    void cancelById(Long id);

    Page<OrderVO> conditionFind(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 设置状态操作可以统一使用update语句
     * 为提高代码可读性暂时分开写
     * @param status
     * @return
     */
    @Select("select count(*) from orders where status=#{status}")
    Integer count(Integer status);
    @Update("update orders set status=3 where id=#{id}")
    void confirm(Long id);
    @Update("update orders set status=6 ,rejection_reason = #{rejectionReason} where id=#{id}")
    void rejection(OrdersRejectionDTO ordersCancelDTO);
    @Update("update orders set status=6 , cancel_reason=#{cancelReason} where id=#{id}")
    void cancelOrders(OrdersCancelDTO ordersCancelDTO);
    @Update("update orders set status=4 where id=#{id}")
    void deliveryById(Long id);
    @Update("update orders set status=5 where id=#{id}")
    void completedById(Long id);
    @Update("update orders set status=6 , cancel_reason='订单超时，系统自动取消' , cancel_time=NOW() where #{now}>=order_time and status=1")
    void cancelByTime(LocalDateTime now);
    @Update("update orders set status=5  , cancel_time=NOW() where #{now}>=order_time and status=4")
    void completeByTime(LocalDateTime now);

    Double sumByMap(Map map);
    @Select("select count(*) from orders")
    Integer allCount();
    @Select("select count(*) from orders where status=5")
    Integer validCount();

    Integer allCountByTime(Map map);

    Integer countByMap(Map map);

    List<GoodsSalesDTO> getSalesTop(LocalDateTime begin, LocalDateTime end);
}
